{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "5DKDTnMGHTGn"
   },
   "source": [
    "# If you are using Colab for free, we highly recommend you activate the T4 GPU\n",
    "# hardware accelerator. Our models are designed to run with at least 16GB\n",
    "# of RAM, activating T4 will grant the notebook 16GB of GDDR6 RAM as opposed\n",
    "# to the ~13GB Colab gives automatically.\n",
    "# To activate T4:\n",
    "# 1. click on the \"Runtime\" tab\n",
    "# 2. click on \"Change runtime type\"\n",
    "# 3. select T4 GPU under Hardware Accelerator\n",
    "# NOTE: there is a weekly usage limit on using T4 for free"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "aBE4SLY5LYiq"
   },
   "source": [
    "##This example aims to show how to run multiple specific queries on text using our slim extract tool, and ouput it as a dictionary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "oanCf6Dn8UlA",
    "outputId": "84e6207b-40bb-47b6-d756-ae6ead743bc1"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: llmware in /usr/local/lib/python3.10/dist-packages (0.3.0)\n",
      "Requirement already satisfied: boto3>=1.24.53 in /usr/local/lib/python3.10/dist-packages (from llmware) (1.34.129)\n",
      "Requirement already satisfied: huggingface-hub>=0.19.4 in /usr/local/lib/python3.10/dist-packages (from llmware) (0.23.3)\n",
      "Requirement already satisfied: numpy>=1.23.2 in /usr/local/lib/python3.10/dist-packages (from llmware) (1.25.2)\n",
      "Requirement already satisfied: pymongo>=4.7.0 in /usr/local/lib/python3.10/dist-packages (from llmware) (4.7.3)\n",
      "Requirement already satisfied: tokenizers>=0.15.0 in /usr/local/lib/python3.10/dist-packages (from llmware) (0.19.1)\n",
      "Requirement already satisfied: psycopg-binary==3.1.17 in /usr/local/lib/python3.10/dist-packages (from llmware) (3.1.17)\n",
      "Requirement already satisfied: psycopg==3.1.17 in /usr/local/lib/python3.10/dist-packages (from llmware) (3.1.17)\n",
      "Requirement already satisfied: pgvector==0.2.4 in /usr/local/lib/python3.10/dist-packages (from llmware) (0.2.4)\n",
      "Requirement already satisfied: colorama==0.4.6 in /usr/local/lib/python3.10/dist-packages (from llmware) (0.4.6)\n",
      "Requirement already satisfied: librosa>=0.10.0 in /usr/local/lib/python3.10/dist-packages (from llmware) (0.10.2.post1)\n",
      "Requirement already satisfied: typing-extensions>=4.1 in /usr/local/lib/python3.10/dist-packages (from psycopg==3.1.17->llmware) (4.12.2)\n",
      "Requirement already satisfied: botocore<1.35.0,>=1.34.129 in /usr/local/lib/python3.10/dist-packages (from boto3>=1.24.53->llmware) (1.34.129)\n",
      "Requirement already satisfied: jmespath<2.0.0,>=0.7.1 in /usr/local/lib/python3.10/dist-packages (from boto3>=1.24.53->llmware) (1.0.1)\n",
      "Requirement already satisfied: s3transfer<0.11.0,>=0.10.0 in /usr/local/lib/python3.10/dist-packages (from boto3>=1.24.53->llmware) (0.10.1)\n",
      "Requirement already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.19.4->llmware) (3.14.0)\n",
      "Requirement already satisfied: fsspec>=2023.5.0 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.19.4->llmware) (2023.6.0)\n",
      "Requirement already satisfied: packaging>=20.9 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.19.4->llmware) (24.1)\n",
      "Requirement already satisfied: pyyaml>=5.1 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.19.4->llmware) (6.0.1)\n",
      "Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.19.4->llmware) (2.31.0)\n",
      "Requirement already satisfied: tqdm>=4.42.1 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.19.4->llmware) (4.66.4)\n",
      "Requirement already satisfied: audioread>=2.1.9 in /usr/local/lib/python3.10/dist-packages (from librosa>=0.10.0->llmware) (3.0.1)\n",
      "Requirement already satisfied: scipy>=1.2.0 in /usr/local/lib/python3.10/dist-packages (from librosa>=0.10.0->llmware) (1.11.4)\n",
      "Requirement already satisfied: scikit-learn>=0.20.0 in /usr/local/lib/python3.10/dist-packages (from librosa>=0.10.0->llmware) (1.2.2)\n",
      "Requirement already satisfied: joblib>=0.14 in /usr/local/lib/python3.10/dist-packages (from librosa>=0.10.0->llmware) (1.4.2)\n",
      "Requirement already satisfied: decorator>=4.3.0 in /usr/local/lib/python3.10/dist-packages (from librosa>=0.10.0->llmware) (4.4.2)\n",
      "Requirement already satisfied: numba>=0.51.0 in /usr/local/lib/python3.10/dist-packages (from librosa>=0.10.0->llmware) (0.58.1)\n",
      "Requirement already satisfied: soundfile>=0.12.1 in /usr/local/lib/python3.10/dist-packages (from librosa>=0.10.0->llmware) (0.12.1)\n",
      "Requirement already satisfied: pooch>=1.1 in /usr/local/lib/python3.10/dist-packages (from librosa>=0.10.0->llmware) (1.8.2)\n",
      "Requirement already satisfied: soxr>=0.3.2 in /usr/local/lib/python3.10/dist-packages (from librosa>=0.10.0->llmware) (0.3.7)\n",
      "Requirement already satisfied: lazy-loader>=0.1 in /usr/local/lib/python3.10/dist-packages (from librosa>=0.10.0->llmware) (0.4)\n",
      "Requirement already satisfied: msgpack>=1.0 in /usr/local/lib/python3.10/dist-packages (from librosa>=0.10.0->llmware) (1.0.8)\n",
      "Requirement already satisfied: dnspython<3.0.0,>=1.16.0 in /usr/local/lib/python3.10/dist-packages (from pymongo>=4.7.0->llmware) (2.6.1)\n",
      "Requirement already satisfied: python-dateutil<3.0.0,>=2.1 in /usr/local/lib/python3.10/dist-packages (from botocore<1.35.0,>=1.34.129->boto3>=1.24.53->llmware) (2.8.2)\n",
      "Requirement already satisfied: urllib3!=2.2.0,<3,>=1.25.4 in /usr/local/lib/python3.10/dist-packages (from botocore<1.35.0,>=1.34.129->boto3>=1.24.53->llmware) (2.0.7)\n",
      "Requirement already satisfied: llvmlite<0.42,>=0.41.0dev0 in /usr/local/lib/python3.10/dist-packages (from numba>=0.51.0->librosa>=0.10.0->llmware) (0.41.1)\n",
      "Requirement already satisfied: platformdirs>=2.5.0 in /usr/local/lib/python3.10/dist-packages (from pooch>=1.1->librosa>=0.10.0->llmware) (4.2.2)\n",
      "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests->huggingface-hub>=0.19.4->llmware) (3.3.2)\n",
      "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests->huggingface-hub>=0.19.4->llmware) (3.7)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests->huggingface-hub>=0.19.4->llmware) (2024.6.2)\n",
      "Requirement already satisfied: threadpoolctl>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from scikit-learn>=0.20.0->librosa>=0.10.0->llmware) (3.5.0)\n",
      "Requirement already satisfied: cffi>=1.0 in /usr/local/lib/python3.10/dist-packages (from soundfile>=0.12.1->librosa>=0.10.0->llmware) (1.16.0)\n",
      "Requirement already satisfied: pycparser in /usr/local/lib/python3.10/dist-packages (from cffi>=1.0->soundfile>=0.12.1->librosa>=0.10.0->llmware) (2.22)\n",
      "Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil<3.0.0,>=2.1->botocore<1.35.0,>=1.34.129->boto3>=1.24.53->llmware) (1.16.0)\n"
     ]
    }
   ],
   "source": [
    "!pip install llmware\n",
    "from llmware.models import ModelCatalog\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "erpOjVoUGZPU"
   },
   "source": [
    "To add more strings to analyze, add them to Text_to_analyze.\n",
    "To add more queries, add them to queries_list.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "id": "Ls6GHIEqF1uq"
   },
   "outputs": [],
   "source": [
    "Text_to_analyze = [\"I am John Doe, I visited Peter, a therapist in the year 2019. I travelled to NYC for around 5 therapy sessions, I was advised to be more outgoing.\"]\n",
    "queries_list = [\"consultant specialty\", \"Consultant name\", \"Consultant location\", \"Consultant Advice\"]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "aWw7uro5C1ym",
    "outputId": "b33fbf63-171c-4b5b-f33a-b102282b42d2"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Query 1: consultant specialty\n",
      "extract response:  {'consultant_specialty': ['Therapist']}\n",
      "Query 2: Consultant name\n",
      "extract response:  {'Consultant_name': ['Peter']}\n",
      "Query 3: Consultant location\n",
      "extract response:  {'Consultant_location': ['NYC']}\n",
      "Query 4: Consultant Advice\n",
      "extract response:  {'Consultant_Advice': ['Be more outgoing.']}\n",
      "output dictionary: \n",
      "{'consultant_specialty': ['Therapist'], 'Consultant_name': ['Peter'], 'Consultant_location': ['NYC'], 'Consultant_Advice': ['Be more outgoing.']}\n"
     ]
    }
   ],
   "source": [
    "#   load the model\n",
    "model = ModelCatalog().load_model(\"slim-extract-tool\",sample=False, temperature=0.0, max_output=200)\n",
    "# loop through the text and queries, calling the model each time\n",
    "output_dict = {}\n",
    "for i, sample in enumerate(Text_to_analyze):\n",
    "    for j, query in enumerate(queries_list):\n",
    "        print(f\"Query {j+1}: {query}\")\n",
    "        response = model.function_call(sample, function=\"extract\", params=[query])\n",
    "        output_dict.update(response[\"llm_response\"])\n",
    "        if response[\"llm_response\"] == []:\n",
    "            print(\"No response\")\n",
    "        print(\"extract response: \", response[\"llm_response\"])\n",
    "\n",
    "\n",
    "    #   display the response on the screen\n",
    "print(\"output dictionary: \" )\n",
    "print(output_dict)"
   ]
  }
 ],
 "metadata": {
  "accelerator": "GPU",
  "colab": {
   "gpuType": "T4",
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3",
   "name": "python3"
  },
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
